#include "MaterialWizard.h"
#include "ui_MaterialWizard.h"

#include <QColor>
#include <QPen>
#include <QPolygonF>
#include <qwt_plot_curve.h>
#include <qwt_symbol.h>
#include <qwt_curve_fitter.h>


MaterialWizard::MaterialWizard(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MaterialWizard)
{
    ui->setupUi(this);

    m_CTtoDensityTable << QPointF(-1000, 0.0)
                       << QPointF(-400, 0.602)
                       << QPointF(-150, 0.924)
                       << QPointF(100, 1.075)
                       << QPointF(300, 1.145)
                       << QPointF(2000, 1.856)
                       << QPointF(4927, 3.379);
    // << QPointF(-5000, 0.0)
                          // << QPointF(-1000, 0.0)

 //   m_Spline.setPoints(m_CTtoDensityTable);
 //   m_Spline.setSplineType(QwtSpline::Natural);

  //  const QColor color(35, 70, 106);

   // QwtPlot *plot = new QwtPlot;
    QPen sinPen = QPen(Qt::red);
    QPen secPen = QPen(Qt::blue);
  //  plot.setCanvasBackground(Qt::white);
   // plot.resize(600,400);


    QwtPlotCurve *curve = new QwtPlotCurve("Densities");
    curve->setRenderHint(QwtPlotItem::RenderAntialiased);
    curve->setPen(sinPen);
    curve->attach(this->ui->materialPlot);

    QwtPlotCurve *curve2 = new QwtPlotCurve("Interpolation");
    curve2->setRenderHint(QwtPlotItem::RenderAntialiased);
    curve2->setPen(secPen);
    curve2->attach(this->ui->materialPlot);


  //  curve->setStyle(QwtPlotCurve::Lines);
  //  curve->setCurveAttribute(QwtPlotCurve::Fitted, true);

  //  QwtSymbol symbol;
  //  symbol.setStyle(QwtSymbol::Rect);
  //  symbol.setSize(10, 10);
  //  symbol.setBrush(color);

  //  curve->setSymbol(symbol);

    QwtSplineCurveFitter *fitter = new QwtSplineCurveFitter();
    fitter->setFitMode(QwtSplineCurveFitter::ParametricSpline);
    QPolygonF fitPoints = fitter->fitCurve(m_CTtoDensityTable);

  //  fitter->setSplineSize(100);
  //  curve->setCurveFitter(fitter);

 //   curve->setRenderHint(QwtPlotItem::RenderAntialiased);
 //   curve->setPen(QPen(color, 3));

    curve->setData(m_CTtoDensityTable);
    curve2->setData(fitPoints);
   // curve->attach(plot);
    this->ui->materialPlot->replot();
}

MaterialWizard::~MaterialWizard()
{
    delete ui;
}
